package org.lmind.example.util;

import static org.junit.Assert.*;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.junit.Test;

public class URLsTestCase {

	@Test
	public void testRelativePath() throws IOException {
		
		String s = URLs.relativePath(new URL("http://example.com/a/a.html"), 
				new URL("http://example.com/b/b.html"));
		assertEquals(s, "../b/b.html");
		
		s = URLs.relativePath(new URL("http://example.com/abc/a.html"), 
				new URL("http://example.com/abc/b.html"));
		assertEquals(s, "b.html");
		
		s = URLs.relativePath(new URL("http://example.com/a/b/c/d.html"), 
				new URL("http://example.com/a/b.html"));
		assertEquals(s, "../../b.html");
		

		s = URLs.relativePath(new URL("http://example.com/a/b.html"), 
				new URL("http://example.com/a/b/c.html"));
		assertEquals(s, "b/c.html");
		
		s = URLs.relativePath(new URL("http://example.com/a/b.html"), 
				new URL("http://example.com"));
		assertEquals(s, "../");
		
		s = URLs.relativePath(new URL("http://example.com/a/b.html"), 
				new URL("http://example2.com/abc/d.html"));
		assertEquals(s, "http://example2.com/abc/d.html");
	}
	
	@Test
	public void testUrl() throws IOException {
		URL url = URLs.url(new URL("http://www.example.com/index.jsp"), "?name=jack");
		assertEquals(url.toString(), "http://www.example.com/index.jsp?name=jack");
		
		url = URLs.url(new URL("http://www.example.com/index.jsp?name=mary"), "?name=jack");
		assertEquals(url.toString(), "http://www.example.com/index.jsp?name=jack");

		url = URLs.url(new URL("http://www.example.com"), "?name=jack");
		assertEquals(url.toString(), "http://www.example.com/?name=jack");

	}

}
